package com.aegis.common.token.handler;

import cn.dev33.satoken.stp.StpInterface;
import cn.hutool.core.collection.CollectionUtil;
import com.aegis.common.redis.util.RedisUtil;
import com.aegis.common.token.constants.CommonTokenProperties;
import com.aegis.common.token.constants.TokenRedisConstants;
import com.aegis.manage.api.dto.SysPermissionDTO;
import com.aegis.manage.api.dto.SysRoleDTO;
import com.aegis.manage.api.service.PermissionServiceApi;
import com.aegis.manage.api.service.RoleServiceApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Author renogo
 * 
 * @Version 1.0
 * @Descritube
 */
@Slf4j
@Component
public class StpInterfaceImpl implements StpInterface {


    @Autowired
    private PermissionServiceApi permissionServiceApi;

    @Autowired
    private RoleServiceApi roleServiceApi;

    @Autowired
    private CommonTokenProperties commonTokenProperties;

    /**
     * StpUtil.getPermissionList()
     *
     * @param loginId   账号id
     * @param loginType 账号类型
     * @return
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        String userId = (String) loginId;
        log.info("开始查询用户权限");
        List<SysPermissionDTO> sysPermissionDTOS = null;
        if (commonTokenProperties.isCacheUserInfo()) {
            sysPermissionDTOS = Optional.ofNullable((List<SysPermissionDTO>) RedisUtil.get(TokenRedisConstants.getUserPermission(userId)))
                    .orElseGet(() -> {
                        log.info("无缓存，开始查询数据库");
                        List<SysPermissionDTO> sysPermissionDOS = permissionServiceApi.selectUserPermissionByUserId(userId);
                        if (sysPermissionDOS == null) {
                            return null;
                        }
                        RedisUtil.set(TokenRedisConstants.getUserPermission(userId), sysPermissionDOS, commonTokenProperties.getCacheUserInfoTime());
                        return sysPermissionDOS;
                    });
        } else {
            sysPermissionDTOS = permissionServiceApi.selectUserPermissionByUserId(userId);
        }
        if (CollectionUtil.isEmpty(sysPermissionDTOS)) {
            return null;
        }
        return sysPermissionDTOS.stream().map(sysPermissionDTO -> sysPermissionDTO.getPermission()).collect(Collectors.toList());
    }


    /**
     * StpUtil.getRoleList()
     *
     * @param loginId   账号id
     * @param loginType 账号类型
     * @return
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        String userId = (String) loginId;
        List<SysRoleDTO> sysRoleDTOS = null;
        if (commonTokenProperties.isCacheUserInfo()) {
            sysRoleDTOS = Optional.ofNullable((List<SysRoleDTO>) RedisUtil.get(TokenRedisConstants.getUserRole(userId)))
                    .orElseGet(() -> {
                        List<SysRoleDTO> role = roleServiceApi.selectRoleByUserId(userId);
                        if (role == null) {
                            return null;
                        }
                        RedisUtil.set(TokenRedisConstants.getUserRole(userId), role, commonTokenProperties.getCacheUserInfoTime());
                        return role;
                    });
        } else {
            sysRoleDTOS = roleServiceApi.selectRoleByUserId(userId);
        }
        if (CollectionUtil.isEmpty(sysRoleDTOS)) {
            return null;
        }
        return sysRoleDTOS.stream().map(sysPermissionDTO -> sysPermissionDTO.getRoleKey()).collect(Collectors.toList());
    }
}
